/*
 * Copyright 2025 The JA-SIG Collaborative. All rights reserved.
 * distributed with this file and available online at
 */
package com.lap.auth.adapter.driven.web.user;

import com.lap.auth.application.user.UserAppService;
import com.lap.auth.application.user.command.CreateUserCmd;
import com.lap.auth.application.user.command.UpdatePasswordCmd;
import com.lap.auth.application.user.command.UpdateStatusCmd;
import com.lap.auth.application.user.command.UpdateUserCmd;
import com.lap.auth.application.user.dto.*;
import com.lap.framework.common.annotation.AuthSkip;
import com.lap.framework.common.dto.page.PageResponse;
import com.lap.watchdog.starter.annotation.RepeatSubmit;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 提供系统管理员restful接口层:
 *
 * <ol>
 *   <li>业务规则，原则上不做任何业务逻辑处理，仅仅是技术入口
 *   <li>命名规则，语义上明确接口做什么，推荐使用 Restful 命名 + Verb风格方法名（适配Swagger、Spring）:
 *       <ul>
 *         <li>分页使用 getXXXPage
 *         <li>获取单个 getXXById
 *         <li>保存使用 createXX
 *         <li>更新使用 updateXX
 *         <li>删除使用 deleteXX
 *       </ul>
 * </ol>
 *
 * @author Shuisheng Lao(劳水生)
 * @version 0.0.1
 */
@Tag(name = "系统管理员管理", description = "提供系统管理员基本操作，查询等")
@RequiredArgsConstructor
@RestController
@RequestMapping("api/v1/users")
public class UserController {

  private final UserAppService userAppService;

  @Operation(summary = "获取用户分页")
  @GetMapping
  public PageResponse<UserWithOrgDto> getUserPage(QueryUserPageQuery query) {
    return userAppService.queryUsersWithOrgByPage(query);
  }

  @Operation(summary = "获取用户下拉")
  @AuthSkip
  @GetMapping("options")
  public PageResponse<UserSimpleDto> getUserOptions(QueryUserSimplePageQuery query) {
    return userAppService.querySimplesByPage(query);
  }

  @Operation(summary = "根据ids获取用户")
  @AuthSkip
  @PostMapping("_batch")
  public List<UserSimpleDto> batchGetSimpleUsers(@RequestBody @Valid GetUserByIdsQuery query) {
    return userAppService.queryUsersByIds(query);
  }

  @Operation(summary = "根据id获取选中用户")
  @AuthSkip
  @GetMapping("{id}/select")
  public UserSimpleDto getSimpleUser(@PathVariable Integer id) {
    return userAppService.getSimpleUserById(id);
  }

  @Operation(summary = "添加用户")
  @RepeatSubmit
  @PostMapping
  public Integer createUser(@RequestBody @Valid CreateUserCmd cmd) {
    return userAppService.createUser(cmd);
  }

  @Operation(summary = "更新用户")
  @RepeatSubmit
  @PutMapping("{id}")
  public void updateUser(@PathVariable Integer id, @RequestBody @Valid UpdateUserCmd cmd) {
    userAppService.updateUser(id, cmd);
  }

  @Operation(summary = "更新密码")
  @RepeatSubmit
  @PatchMapping("{id}")
  public void updatePassword(@PathVariable Integer id, @RequestBody @Valid UpdatePasswordCmd cmd) {
    userAppService.changePassword(id, cmd.getPassword());
  }

  @Operation(summary = "启用用户")
  @RepeatSubmit
  @PatchMapping("{id}/enable")
  public void enableUser(@PathVariable Integer id, @RequestBody UpdateStatusCmd cmd) {
    userAppService.toggleUserStatus(id, cmd.getRemark(), cmd.getVersion(), true);
  }

  @Operation(summary = "禁用用户")
  @RepeatSubmit
  @PatchMapping("{id}/disable")
  public void disableUser(@PathVariable Integer id, @RequestBody UpdateStatusCmd cmd) {
    userAppService.toggleUserStatus(id, cmd.getRemark(), cmd.getVersion(), false);
  }

  @Operation(summary = "删除用户")
  @RepeatSubmit
  @DeleteMapping("{id}")
  public void deleteUser(@PathVariable Integer id) {
    userAppService.deleteUser(id);
  }
}
